package com.ossean.main;

import com.ossean.dao.OpenSourceProjectsDao;
import com.ossean.dao.RelativeMemoToOpenSourceProjectDao;
import com.ossean.dao.SimilarityDao;
import com.ossean.model.OpenSourceProject;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.*;
import java.util.Map.Entry;

/**
 * Created by zyr on 17/9/3.
 */
@Component
public class SimilityCount {

	@Resource
    private RelativeMemoToOpenSourceProjectDao  relativeMemoToOpenSourceProjectDao ;
    @Resource
    private OpenSourceProjectsDao openSourceProjectsDao;
    @Resource
    private SimilarityDao similarityDao;

    private String openSourceProjectTable = "open_source_projects";

    @Transactional(propagation=Propagation.REQUIRES_NEW)
    public void getSimilarOsp(int startId,int endId){

    	List<OpenSourceProject> prjList = openSourceProjectsDao.getOpenSourceProjectTagsList(openSourceProjectTable,startId,endId);
		int osp;
		String osp_tags;

    	long start_time = System.currentTimeMillis();
		int ospCount = 0;

		//traverse list
    	for(int i = 0;i < prjList.size();i++){
			osp = prjList.get(i).getId();
			osp_tags = prjList.get(i).getTags();
			if(osp_tags==null)
				continue;
			String [] tag_array = osp_tags.split(",");

			Map<Integer,Integer> map = new HashMap();
			for(String tag_ : tag_array){
				List<Integer> relaPrjList = openSourceProjectsDao.getOpenSourceProjectList4SameTag
						(openSourceProjectTable,tag_,osp);
				for(int relaPrj:relaPrjList){
					if(map.containsKey(relaPrj))
						map.put(relaPrj,map.get(relaPrj)+1);
					else
						map.put(relaPrj,1);
				}
			}

			for(Map.Entry<Integer, Integer> entry : map.entrySet())
				similarityDao.insertRecommends("simility_recommends",osp,entry.getKey(),entry.getValue());

			ospCount++;
			System.out.println("current handled prj: "+osp);
			if(ospCount % 500==0) {
				System.out.println("have handled: " + ospCount + " projects");
				long current_time = System.currentTimeMillis();
				System.out.println("cost: "+(current_time - start_time)/1000/60+" mins");
			}

    	}

    }
    
    public float compareTags(String tag1,String tag2){
    	String [] tag_1_array = tag1.split(",");
    	String [] tag_2_array = tag2.split(",");
    	int intersectSize = intersect(tag_1_array, tag_2_array);
    	float minSize = tag_1_array.length < tag_2_array.length ? tag_1_array.length:tag_2_array.length;
    	if(minSize !=0)
    		return (intersectSize/minSize);
    	else
    		return 0;
    }
    
  //求两个数组的交集   
    public int intersect(String[] arr1, String[] arr2) {   
        Map<String, Boolean> map = new HashMap<String, Boolean>();   
        LinkedList<String> list = new LinkedList<String>();   
        for (String str : arr1) {   
            if (!map.containsKey(str)) {   
                map.put(str, Boolean.FALSE);   
            }   
        }   
        for (String str : arr2) {   
            if (map.containsKey(str)) {   
                map.put(str, Boolean.TRUE);   
            }   
        }   
  
        for (Entry<String, Boolean> e : map.entrySet()) {   
            if (e.getValue().equals(Boolean.TRUE)) {   
                list.add(e.getKey());   
            }   
        }   
  
        String[] result = {};   
        return list.size();   
    }

	public  String getRecommendTable(int ospId){
		int a = ospId % 100;
		return "simility_recommends_" + a;
	}
    
    public static void main(String [] args){
    	ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/applicationContext*.xml");
    	SimilityCount mainClass = applicationContext.getBean(SimilityCount.class);
    	mainClass.getSimilarOsp(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
    }

}